package com.abc.easyclaim;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.ImageButton;
import android.widget.TextView;

public class U2UpdateReceiver extends BroadcastReceiver {

	int status=Constant.STATUS_STOPRECORD;   //初始状态为停止录音状态
	private int record_time;       //记录文件录制时间长度
	private int defaultNum;               //要录音的默认文件序号
	
	Activity ac;//此Receiver对应的Activity
    public U2UpdateReceiver(Activity ac)
    {
    	this.ac=ac; 
    }
	
	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		//从接收的Intent中获取状态值
		status=intent.getIntExtra("status", -1);
		record_time=intent.getIntExtra("record_time", 0);
		defaultNum=intent.getIntExtra("defaultNum", -1);
		
		ImageButton recordbtn=(ImageButton)ac.findViewById(R.id.record);
		ImageButton stopbtn=(ImageButton)ac.findViewById(R.id.stop);
		TextView stateTextView=(TextView)ac.findViewById(R.id.record_state);          //状态显示文本
		TextView recordTimeTextView=(TextView)ac.findViewById(R.id.time);        //录音时间长度显示文本
		switch (status) {
		case Constant.STATUS_RECORD:     //更新到录音状态
			recordbtn.setImageResource(R.drawable.play_pause);
			stateTextView.setText("正在录制   ");
			System.out.println("record_defaultNum="+defaultNum);
			if (defaultNum!=-1) {         //新一轮录音
				//stopbtn.setImageResource(R.drawable.p);
				stopbtn.setClickable(true);
				stateTextView.setVisibility(0);         //设为可见
				recordTimeTextView.setVisibility(0);
			}
			break;
		case Constant.STATUS_PAUSERECORD:       //更新到暂停状态
			recordbtn.setImageResource(R.drawable.play_go);
			stateTextView.setText("暂停录制   ");
			break;
			
		case Constant.STATUS_STOPRECORD:        //更新到停止状态
			recordbtn.setImageResource(R.drawable.play_go);
			//stopbtn.setImageResource(R.drawable.grey_stop);
			stopbtn.setClickable(false);
			stateTextView.setVisibility(8);              //设为不可见
			recordTimeTextView.setVisibility(8);
			break;
		}
		System.out.println("this.record_time="+record_time);
		recordTimeTextView.setText(getRecordTime(record_time));
	}

	//获取已录制时间字符函数
    public String getRecordTime(int time)
    {
    	String result=null;
    	int hour=time/3600;
    	int minutes=(time-hour*3600)/60;
    	int seconds=time-hour*3600-minutes*60;
    	result=hour+":"+
    	        ((minutes<10)?("0"+minutes):minutes)+":"+                //变成标准的形式：0:00:00
    			((seconds<10)?("0"+seconds):seconds);
		return result;    	
    }
}
